// SubgroupDisplay.cpp: implementation of the SubgroupDisplay class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwclientcore.h"
#include "SubgroupDisplay.h"

#include "CommonLib\Markup.h"
#include "CommonLib\Structure.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SubgroupDisplay::SubgroupDisplay(long nCoalMineId):
m_nCoalMineId(nCoalMineId)
{

}

SubgroupDisplay::~SubgroupDisplay()
{
  DeleteAllMonUnitId();
}

void SubgroupDisplay::DeleteAllMonUnitId()
{
  m_vecMonUnitId.clear();
}

void SubgroupDisplay::LoadContentFromXML(Markup& xml)
{
  if (xml.FindChildElem(_T("NumOfMonUnitId")))
  {
    int nNumOfMonUnitId = atoi(xml.GetChildData());
    for (int i = 0;i < nNumOfMonUnitId;i++)
    {
      if (xml.FindChildElem(_T("MonUnitId")))
      {
        CString strMonUnitId = xml.GetChildData();
        m_vecMonUnitId.push_back(strMonUnitId);
      }
    }
  }
}
void SubgroupDisplay::LoadContentFromXMLNew(Markup& xml,long nCoalMineId)
{
  if (xml.FindChildElem(_T("NumOfMonUnitId")))
  {
    int nNumOfMonUnitId = atoi(xml.GetChildData());
    for (int i = 0;i < nNumOfMonUnitId;i++)
    {
      if (xml.FindChildElem(_T("MonUnitId")))
      {
        //CString strMonUnitId = xml.GetChildData();
        //m_vecMonUnitId.push_back(strMonUnitId);
			SubMonitoringUnit mSubMonitoringUnit;
			mSubMonitoringUnit.nId = nCoalMineId;
			mSubMonitoringUnit.strMonUnitId = xml.GetChildData();;
			m_vecSubMonitoringUnit.push_back(mSubMonitoringUnit);
      }
    }
  }
}

void SubgroupDisplay::SaveContentToXML(Markup& xml)
{
  CString str;
  str.Format("%d", m_vecMonUnitId.size());
  xml.AddChildElem(_T("NumOfMonUnitId"), _T(str));
  for (int i = 0;i < m_vecMonUnitId.size();i++)
  {
    xml.AddChildElem(_T("MonUnitId"), _T(m_vecMonUnitId[i]));
  }
}
void SubgroupDisplay::SaveContentToXMLNew(Markup& xml,long nCoalMineId)
{
	DeleteAllMonUnitId();
	int count = m_vecSubMonitoringUnit.size(); 
	int i;
	for( i=0;i<count;i++)
	{
		if(m_vecSubMonitoringUnit[i].nId == nCoalMineId ){
			 m_vecMonUnitId.push_back(m_vecSubMonitoringUnit[i].strMonUnitId );
		}
	}
	SaveContentToXML( xml);
}

void SubgroupDisplay::GetMonUnitItems(std::vector<MonUnitItem *>& vecMonUnitItems) const
{
  for (int i = 0;i < m_vecMonUnitId.size();i++)
  {
    MonUnitItem * pMonUnitItem = new MonUnitItem;
    pMonUnitItem->m_nCoalMineId = m_nCoalMineId;
    strcpy(pMonUnitItem->m_szMonUnitId, m_vecMonUnitId[i]);
    vecMonUnitItems.push_back(pMonUnitItem);
  }
}

void SubgroupDisplay::NewMonUnitId(const CString& strMonUnitId)
{
	std::vector<CString>::const_iterator iter = std::find(m_vecMonUnitId.begin(), m_vecMonUnitId.end(), strMonUnitId);
	if (iter == m_vecMonUnitId.end())
	{
		m_vecMonUnitId.push_back(strMonUnitId);
	}
}

void SubgroupDisplay::RemoveMonUnitId(const CString& strMonUnitId)
{
	m_vecMonUnitId.erase(std::remove(m_vecMonUnitId.begin(), m_vecMonUnitId.end(), strMonUnitId), m_vecMonUnitId.end());
}

void SubgroupDisplay::NewSubMonitoringUnit(long nCoalMineId, const CString& strMonUnitId)
{
	SubMonitoringUnit mSubMonitoringUnit;
	mSubMonitoringUnit.nId = nCoalMineId;
	mSubMonitoringUnit.strMonUnitId = strMonUnitId;
	int count = m_vecSubMonitoringUnit.size(); 
	int i;
	for( i=0;i<count;i++)
	{
		if(m_vecSubMonitoringUnit[i].nId == nCoalMineId && m_vecSubMonitoringUnit[i].strMonUnitId == strMonUnitId){
			break;
		}
		
	}
	if(i==count)m_vecSubMonitoringUnit.push_back(mSubMonitoringUnit);
}
void SubgroupDisplay::RemoveSubMonitoringUnit(long nCoalMineId, CString& strMonUnitId)
{
	int count = m_vecSubMonitoringUnit.size(); 
	for(int i=0;i<count;i++)
	{
		if(m_vecSubMonitoringUnit[i].nId == nCoalMineId && m_vecSubMonitoringUnit[i].strMonUnitId == strMonUnitId)
		{
			std::vector<SubMonitoringUnit>::iterator iter = &m_vecSubMonitoringUnit[i];
			m_vecSubMonitoringUnit.erase(iter);
			break;
		}
	}
}

void SubgroupDisplay::SetMonUnitIdVectorNew(const std::vector<CString>& vecMonUnitId,long nCoalMineId)
{

	DeleteAllMonUnitId();
	m_vecMonUnitId = vecMonUnitId;
	int count = m_vecMonUnitId.size(); 
	int i;
	for( i=0;i<count;i++)
	{
		SubMonitoringUnit mSubMonitoringUnit;
		mSubMonitoringUnit.nId = nCoalMineId;
		mSubMonitoringUnit.strMonUnitId = m_vecMonUnitId[i];
		m_vecSubMonitoringUnit.push_back(mSubMonitoringUnit);
	}
    int nAllcount = m_vecSubMonitoringUnit.size();
}